package com.xiaochong.tir.manager.controller;

import com.xiaochong.tir.common.data.po.ExchangeInfo;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.po.TransactionPair;
import com.xiaochong.tir.manager.service.ExchangeInfoService;
import com.xiaochong.tir.manager.service.ExchangeTransactionpairMiddleService;
import com.xiaochong.tir.manager.service.TransactionPairService;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: WangXing
 * @Description: 插入交易对表 及 交易所、交易对中间表数据
 * @CreateDate: 2018/6/29 10:58
 */
@Api(value = "插入数据")
@RestController
@RequestMapping("/manage/")
public class JsonController {

    @Autowired
    private ExchangeTransactionpairMiddleService exchangeTransactionpairMiddleService;
    @Autowired
    private TransactionPairService transactionPairService;
    @Autowired
    private ExchangeInfoService exchangeInfoService;

    @ApiOperation(value = "插入数据",notes = "插入数据")
    @GetMapping("insJson")
    public Object insertTra(){
        ExchangeTransactionpairMiddle etm = null;
        TransactionPair tra = null;
        ExchangeInfo info = new ExchangeInfo();
        TransactionPair trao = null;
        try {
            File file=new File("E:/ideaIU/work/tirservice/tir-manage/src/main/java/com/xiaochong/tir/manager/util/tra.json");
            String content= FileUtils.readFileToString(file,"UTF-8");
            JSONObject jsonObject = new JSONObject(content);
            Iterator it = jsonObject.keys();
            //交易对表
            while (it.hasNext()){
                String key = (String)it.next();
                JSONArray array = jsonObject.getJSONArray(key);
                //查询交易所id
                info.setExchangeAliasName(key);
                int exchangeId = exchangeInfoService.selectByEntity(info).getId();
                for (int i = 0; i < array.length(); i++) {
                    String[] name = String.valueOf(array.get(i)).toUpperCase().trim().split("/");
                    tra = new TransactionPair();
                    tra.setEnName(name[1]);
                    tra.setTransactionName(name[0]);
                    //查询交易对表中交易对是否存在
                    trao = transactionPairService.selectByEntity(tra);
                    //交易对表
                    if(trao == null){
                        SessionUtil.setSessionAttribute(SessionUtil.LOGINNAME,"新增交易对");
                        tra.setCreateTime(new Date());
                        tra.setUpdateTime(new Date());
                        tra.setImageUrl(null);
                        transactionPairService.insertTransaction(tra);
                    }
                    //交易对中间表
                    trao = transactionPairService.selectByEntity(tra);
                    if(trao != null){
                        etm = new ExchangeTransactionpairMiddle();
                        etm.setExchangeId(exchangeId);
                        etm.setTransactionId(trao.getId());
                        etm.setTransactionName(name[0]+"/"+name[1]);
                        etm.setTransactionAlias(name[0]+"/"+name[1]);
                        //查询中间表中，交易所是否存在该交易对，若不存在就添加
                        ExchangeTransactionpairMiddle etmo = exchangeTransactionpairMiddleService.selectByEntity(etm);
                        if(etmo == null){
                            SessionUtil.setSessionAttribute(SessionUtil.LOGINNAME,"新增交易对中间表");
                            etm.setSelected(0);
                            etm.setCreateTime(new Date());
                            etm.setEnableFlag(false);
                            etm.setUpdateTime(new Date());
                            etm.setQuantifyCurrencySelect(false);
                            etm.setIsDepth(false);
                            exchangeTransactionpairMiddleService.insertSelective(etm);
                        }
                    }
                }
            }
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        } catch (JSONException e) {
            e.printStackTrace();
            return 0;
        }
    }

/*    @ApiOperation(value = "修改数据",notes = "修改数据")
    @PostMapping("updatetra")
    public Object updateTra(){
       List<TransactionPair> transactionPairs =  transactionPairService.selectByTraList(new TransactionPair());
       if(null != transactionPairs){
           for (int i = 0; i < transactionPairs.size(); i++) {
               TransactionPair tra = transactionPairs.get(i);
               if(tra.getEnName().contains("/")){
                   String [] arr = tra.getEnName().split("/");
                   tra.setTransactionName(arr[0]);
                   tra.setEnName(arr[1]);
                   tra.setUpdateTime(new Date());
                   transactionPairService.updateByPrimaryKey(tra,tra);
               }
           }
       }
       return 1;
    }*/
}
